package com.fantabest.common.client;

import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreFilter;
import com.fantabest.common.entity.Manager;
import com.fantabest.common.entity.Player;
import com.fantabest.common.entity.Season;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class StoreManager {

	private static StoreManager instance = null;

	public static StoreManager getInstance() {
		if (instance == null) {
			instance = new StoreManager();
		}
		return instance;
	}

	/**
	 * Ritorna uno store con tutte le stagioni
	 * 
	 * @return
	 */
	public ListStore<BeanModel> getSeasonStore() {
		ListStore<BeanModel> store = Registry.get(RegistryKeys.SEASON_STORE);
		if (store == null) {
			final SeasonServiceAsync service = Registry
					.get(RegistryKeys.SEASON_SERVICE);
			RpcProxy<List<Season>> proxy = new RpcProxy<List<Season>>() {
				@Override
				public void load(Object loadConfig,
						AsyncCallback<List<Season>> callback) {
					service.findAll(callback);
				}
			};
			BeanModelReader reader = new BeanModelReader();
			ListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(
					proxy, reader);
			store = new ListStore<BeanModel>(loader);
			Registry.register(RegistryKeys.SEASON_STORE, store);
		}
		return store;
	}

	/**
	 * Ritorna uno store di players di un determinato ruolo
	 * 
	 * @return
	 */
	public ListStore<BeanModel> getPlayersStore(String position) {
		final String p = position;
		final PlayerServiceAsync service = Registry
				.get(RegistryKeys.PLAYER_SERVICE);
		// proxy and reader
		RpcProxy<List<Player>> proxy = new RpcProxy<List<Player>>() {
			@Override
			public void load(Object loadConfig,
					AsyncCallback<List<Player>> callback) {
				service.findByPosition(p, callback);
			}
		};
		BeanModelReader reader = new BeanModelReader();

		// loader and store
		ListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(
				proxy, reader);
		ListStore<BeanModel> store = new ListStore<BeanModel>(loader);
		return store;
	}

	/**
	 * Ritorna uno store di players di un determinato ruolo
	 * 
	 * @return
	 */
	public ListStore<BeanModel> getTeamPlayersStore() {

		final TeamServiceAsync service = Registry
				.get(RegistryKeys.TEAM_SERVICE);
		final Manager manager = Registry.get(RegistryKeys.MANAGER_LOGGED_IN);
		final Season season = Registry.get(RegistryKeys.SEASON_ENTITY);
		// proxy and reader
		RpcProxy<List<Player>> proxy = new RpcProxy<List<Player>>() {
			@Override
			public void load(Object loadConfig,
					AsyncCallback<List<Player>> callback) {
				service.findPlayers(manager, season, callback);
			}
		};
		BeanModelReader reader = new BeanModelReader();

		// loader and store
		ListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(
				proxy, reader);
		ListStore<BeanModel> store = new ListStore<BeanModel>(loader);

		StoreFilter<BeanModel> filter = new StoreFilter<BeanModel>() {
			public boolean select(Store<BeanModel> store, BeanModel parent,
					BeanModel item, String property) {
				Player player = item.getBean();
				if (!player.getPosition().equalsIgnoreCase(property)) {
					return false;
				}
				return false;
			}
		};
		store.addFilter(filter);
		return store;
	}

}
